/**
 * 
 */
package br.com.ufrn.aipetro.ga.view.maintenance;

import java.util.Date;
import java.util.GregorianCalendar;

import java.beans.PropertyChangeEvent;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;


import br.com.ufrn.aipetro.ga.controller.device.ControllerController;
import br.com.ufrn.aipetro.ga.controller.maintenance.ServiceNoteController;
import br.com.ufrn.aipetro.ga.domain.entity.device.Device;
import br.com.ufrn.aipetro.ga.domain.entity.device.controller.Controller;
import br.com.ufrn.aipetro.ga.domain.entity.device.maintenance.Cause;
import br.com.ufrn.aipetro.ga.domain.entity.device.maintenance.MaintenanceType;
import br.com.ufrn.aipetro.ga.domain.entity.device.maintenance.Team;
import br.com.ufrn.aipetro.ga.domain.entity.mesh.Mesh;
import br.com.ufrn.aipetro.ga.domain.entity.plant.Plant;
import br.com.ufrn.aipetro.ga.domain.entity.unit.Unit;
import br.com.ufrn.aipetro.ga.view.interfaces.View;
import br.com.ufrn.aipetro.ga.model.persist.hibernate.HibernateDaoFactory;

/**
 * @author Andr�
 *
 */
public class MaintenanceScheduleFxmlController implements View<ServiceNoteController>, Initializable {

	private static final int INSERT_MODE = 0;
	private static final int UPDATE_MODE = 1;
	
	private int editMode;
	private ServiceNoteController controller;
	
	@FXML private ComboBox<Mesh> cbbMesh;
	@FXML private ComboBox<Plant> cbbPlant;
	@FXML private ComboBox<Unit> cbbUnit;
	@FXML private ComboBox<Device> cbbDevice;
	@FXML private ComboBox<Cause> cbbCause;
	@FXML private ComboBox<MaintenanceType> cbbTypeMaintenance;
	@FXML private ComboBox<Team> cbbTeam;
	@FXML private ComboBox<Integer> cbbPriority;
	//@FXML private ComboBox<MaintenanceType> cbbPriority;
	@FXML private TextField tdfTitle;
	@FXML private TextField tdfDateStart;
	@FXML private TextField tdfDateFinish;
	@FXML private TextField tdfDescription;

	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		//GregorianCalendar calendar = new GregorianCalendar(2013, 3, 28);
		//SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		//tfdDateStart.setText(format.format(calendar.getTime()));
		loadCbbUnit();
		loadCbbTypeMaintenances();
		loadCbbCause();
		loadCbbTeam();
	}
	
	public MaintenanceScheduleFxmlController() {
		editMode = INSERT_MODE;
		initView(new ServiceNoteController(new HibernateDaoFactory()));
	}
	
	@FXML
	private void saveForm() {
		if (this.editMode == INSERT_MODE)
			insertController();
		else
			updateController();
		
	}
	
	private void insertController()  {
		loadFormToMaintenance();
		controller.persistInsert();
		setUpdateMode();
	}
	
	private void updateController()  {
		loadFormToMaintenance();
		controller.persistUpdate();
	}
	
	@FXML
	private void cancelForm() {
		
	}
	
	/**
	 * Povoa o combo box com a lista dos status de operacao dispon�veis 
	 */
	private void loadCbbTypeMaintenances() {
		cbbTypeMaintenance.getItems().clear();
		cbbTypeMaintenance.getItems().addAll(controller.persistSearchAllMaintenanceType());
	}
	
	private void loadCbbUnit() {
		cbbUnit.getItems().clear();
		cbbUnit.getItems().addAll(controller.persistSearchAllUnit());
	}
	
	private void loadCbbPlant() {
		cbbPlant.getItems().clear();
		cbbPlant.getItems().addAll(controller.persistSearchPlantByUnit(cbbUnit.getSelectionModel().getSelectedItem()));
	}
	
	private void loadCbbMesh() {
		cbbMesh.getItems().clear();
		cbbMesh.getItems().addAll(controller.persistSearchMeshByPlant((cbbPlant.getSelectionModel().getSelectedItem())));
	}
	
	private void loadCbbDeviceTag() {
		cbbDevice.getItems().clear();
		cbbDevice.getItems().addAll(controller.persistSearchAllDeviceTag((cbbMesh.getSelectionModel().getSelectedItem())));
	}
	
	private void loadCbbTeam() {
		cbbTeam.getItems().clear();
		cbbTeam.getItems().addAll(controller.persistSearchAllTeam());
	}
	
	private void loadCbbPriority() {
		cbbPriority.getItems().clear();
		//cbbPriority.getItems().addAll(controller.persistSearchAllDeviceTag((cbbMesh.getSelectionModel().getSelectedItem())));
	}
	
	private void loadCbbCause() {
		cbbCause.getItems().clear();
		cbbCause.getItems().addAll(controller.persistSearchAllCause());
	}
	
	private void loadMaintenanceToForm() {
		this.tdfTitle.setText(controller.getModel().getTitle());
		this.tdfDescription.setText(controller.getModel().getDescription());
		this.tdfDateStart.setText(controller.getModel().getDateStart().toString());
		this.tdfDateFinish.setText(controller.getModel().getDateFinish().toString());
		this.cbbTypeMaintenance.getSelectionModel().select(controller.getModel().getMaintenanceType());
		this.cbbCause.getSelectionModel().select(controller.getModel().getCause());
		this.cbbTeam.getSelectionModel().select(controller.getModel().getTeam());
		this.cbbDevice.getSelectionModel().select(controller.getModel().getDevice());
		//this.cbbMesh.getSelectionModel().select(controller.getModel().geMesh());
		//this.cbbPlant.getSelectionModel().select(controller.getModel().);
		
	}
	
	private void loadFormToMaintenance()  {
		controller.changeTitle(this.tdfTitle.getText());
		controller.changeDescription(this.tdfDescription.getText());
		controller.changeDevice(this.cbbDevice.getSelectionModel().getSelectedItem());
		String data = tdfDateStart.getText();
		Date dateStart = new GregorianCalendar(2013, 6, 02).getTime();
		controller.changeDateStart(dateStart);
		Date dateFinish = new GregorianCalendar(2013, 6, 30).getTime();
		controller.changeDateFinish(dateFinish);
		controller.changeTypeMaintenance(this.cbbTypeMaintenance.getSelectionModel().getSelectedItem());
		controller.changeCause(this.cbbCause.getSelectionModel().getSelectedItem());
		controller.changeTeam(this.cbbTeam.getSelectionModel().getSelectedItem());
	}
	

	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void initView(ServiceNoteController controller) {
		this.controller = controller;
		this.controller.registerView(this);
		
	}
	
	private Date loadDate(String tfdDate) throws ParseException {
	    DateFormat formatter = new SimpleDateFormat("MM/dd/yy");  
	    Date date = (Date)formatter.parse(tfdDate);
	    return date;
	}

	@Override
	public void modelChange() {
		setUpdateMode();
		loadMaintenanceToForm();
	}
	
	private void setUpdateMode() {
		this.editMode = UPDATE_MODE;
	}
	
	@FXML
	public void selectMesh() {
		loadCbbDeviceTag();
	}
	
	@FXML
	public void selectPlant() {
		loadCbbMesh();
	}
	
	@FXML
	public void selectUnit() {
		loadCbbPlant();
	}

}
